﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MapAndCharacter_Filip
{
    public partial class ControlPanel : Form
    {
        int monsterCount = 100;
        public string ShowMyDialogBox(bool username = false)
        {
            TextBox testDialog = new TextBox();
            if (username == true)
            {
                testDialog.label1.Text = "Type username below: ";
                testDialog.Text = "Username";
            }
            // Show testDialog as a modal dialog and determine if DialogResult = OK.
            if (testDialog.ShowDialog(this) == DialogResult.OK)
            {
                // Read the contents of testDialog's TextBox.
                testDialog.Dispose();
                return testDialog.textBox1.Text;
            }
            else
            {
                testDialog.Dispose();
                return "";
            }
        }

        public ControlPanel()
        {
            InitializeComponent();
        }

        private void flowLayoutPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void ConnectButton_Click(object sender, EventArgs e)
        {
            string ipAddress;
            if (!GameState.IsHost)
                ipAddress = ShowMyDialogBox();
            else
                ipAddress = HostThread.localAddr.ToString();
            
            TcpClient socket;
            try
            {
                socket = new TcpClient(ipAddress, NetworkManager.port);
            }
            catch (Exception ex) { return; }

            ConnectButton.Text = "Connected!";
            ConnectButton.Enabled = false;
            DisconnectButton.Enabled = true;
            HostButton.Enabled = false;

            GameState.client = new ClientThread(ref socket);
            GameState.client.StartListening();
            Console.WriteLine("My ID: " + GameState.client.ID);
            ChatForm chatForm = new ChatForm();
            chatForm.StartPosition = FormStartPosition.Manual;
            chatForm.Location = new System.Drawing.Point(10, 650);
            Thread chatThread = new Thread(() => Application.Run(chatForm));
            DistanceInfo distanceForm = new DistanceInfo();
            distanceForm.StartPosition = FormStartPosition.Manual;
            distanceForm.Location = new System.Drawing.Point(830, 175);
            Thread distThread = new Thread(() => Application.Run(distanceForm));
            if (GameState.IsHost.Equals(true))
            {
                HostControls hostForm = new HostControls();
                hostForm.StartPosition = FormStartPosition.Manual;
                hostForm.Location = new System.Drawing.Point(830, 340);
                Thread hCThread = new Thread(() => Application.Run(hostForm));
                hCThread.Start();
            }
            chatThread.Start();
            distThread.Start();
            GameState.GameStarted = true;
            if (GameState.username.Equals("Player"))
                GameState.username = "Player" + GameState.client.ID;
        }

        private void HostButton_Click(object sender, EventArgs e)
        {
            GameState.IsHost = true;
            Thread hostThread = new Thread(() => HostThread.Listen());
            hostThread.Start();

            HostButton.Text = HostThread.localAddr.ToString();
            HostButton.Enabled = false;
            DisconnectButton.Enabled = true;
            ChangeMapButton.Enabled = true;
            ConnectButton_Click(null, null);
            CreateMonsterButton.Enabled = true;
            UploadCharacterButton.Enabled = false;
        }

        private void UploadCharacterButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;
                if (filename.Contains(".jpeg") || filename.Contains(".jpg"))
                {

                    using (FileStream fileStream = new FileStream(filename, FileMode.Open))
                    {
                        GameState.game.characters[GameState.client.ID]
                            .SetImage(Texture2D.FromStream(GameState.game.GraphicsDevice, fileStream));
                    }

                    NetworkManager.SendCharacter(filename);
                }
            }
        }

        private void CharacterCreationButton_Click(object sender, EventArgs e)
        {
            Character_Creator charCreator = new Character_Creator();
            charCreator.StartPosition = FormStartPosition.Manual;
            charCreator.Location = new System.Drawing.Point(830, 425);
            Thread characterThread = new Thread(() => Application.Run(charCreator));
            characterThread.Start();
        }

        private void CreateMonsterButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;
                if (filename.Contains(".jpeg") || filename.Contains(".jpg"))
                {

                    using (FileStream fileStream = new FileStream(filename, FileMode.Open))
                    {
                        MovableSprite character = new MovableSprite(GameState.game.Content, GameState.game.spriteBatch);
                        character.Load("TempCharacter");
                        character.Position = new Vector2(20, 20);
                        monsterCount++;
                        GameState.game.characters.Add(monsterCount, character);
                        GameState.game.characters[monsterCount]
                            .SetImage(Texture2D.FromStream(GameState.game.GraphicsDevice, fileStream));
                    }
                    
                    NetworkManager.SendCharacter(filename, monsterCount);
                }
            }
        }

        private void ChangeMapButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Console.WriteLine(openFileDialog.FileName);
                string filename = openFileDialog.FileName;
                if (filename.Contains(".jpeg") || filename.Contains(".jpg"))
                {
                    GameState.IsNewMap = true;
                    GameState.mapImage = filename;
                    NetworkManager.SendMap(filename);
                }
            }
            ScalePromt scaleDialog = new ScalePromt();

            // Show testDialog as a modal dialog and determine if DialogResult = OK.
            if (scaleDialog.ShowDialog(this) == DialogResult.OK)
            {
                scaleDialog.Dispose();
                GameState.scale = Int32.Parse(scaleDialog.textBox1.Text);
            }
        }

        private void DisconnectButton_Click(object sender, EventArgs e)
        {
            ConnectButton.Text = "Connect";
            ConnectButton.Enabled = true;
            HostButton.Text = "Host a Game";
            HostButton.Enabled = true;
            DisconnectButton.Enabled = false;
            ChangeMapButton.Enabled = false;
            if (GameState.IsHost)
                HostThread.Close();
            GameState.client.Close();
        }

        private void UsernameButton_Click(object sender, EventArgs e)
        {
            string user = ShowMyDialogBox(true);
            Console.WriteLine("Username: " + user);
            GameState.username = user;
        }
    }
}